package com.water.bs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.water.bs.pojo.Organization;
import com.water.bs.pojo.User;
import com.water.bs.service.OrgService;
import com.water.bs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrgService orgService;
    @RequestMapping("/login")
    public JSON login(@RequestBody JSONObject loginInfo){
        JSONObject rs = new JSONObject();
        String userCaptcha = (String) loginInfo.get("captcha");
        String rightCaptcha = CodeController.getCaptcha();
        System.out.println("接收login信息:"+loginInfo);
        System.out.println("正确的验证码:"+rightCaptcha);
        if (!rightCaptcha.equals(userCaptcha)){
            rs.put("state","2");
            rs.put("message","验证码错误!");
            return rs;
        }
        Map<String,Object> map = new HashMap<>();
        map = loginInfo;
        //密码转MD5
        String s = (String) map.get("password");
        map.put("password", DigestUtils.md5DigestAsHex(s.getBytes()));
        String lowerCaseEmail = ((String) loginInfo.get("emailOrPhone")).toLowerCase(); //登入信息的邮箱变成小写
        map.put("emailOrPhone",lowerCaseEmail);
        User loginUser = null;
        loginUser = userService.login(map);
        System.out.println("登入账号信息:"+loginUser);
        if (loginUser !=null){
            rs.put("state","0");
            rs.put("message","登入成功!");
            rs.put("loginUser",JSON.toJSONString(loginUser));
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","账号或密码错误!");
            return rs;
        }
    }


    @RequestMapping("keepLogin")
    public JSON keepLogin(@RequestBody JSONObject loginInfo){
        JSONObject rs = new JSONObject();
        System.out.println("验证keepLogin信息:"+loginInfo);
        Map<String,Object> map = new HashMap<>();
        map = loginInfo;
        User loginUser = null;
        loginUser = userService.login(map);
        if (loginUser !=null){
            rs.put("state","0");
            rs.put("message","登入成功!");
            rs.put("loginUser",JSON.toJSONString(loginUser));
            return rs;
        }else{
            rs.put("state","3");
            rs.put("message","您未登入或登入信息已失效!");
            return rs;
        }
    }

    @RequestMapping("/orgLogin")
    public JSON orgLogin(@RequestBody JSONObject orgLoginInfo){
        System.out.println("登入信息:"+orgLoginInfo);
        JSONObject rs = new JSONObject();
        String orgCaptcha = (String) orgLoginInfo.get("captcha");
        String rightCaptcha = CodeController.getCaptcha();
        System.out.println("正确的验证码:"+rightCaptcha);
        if (!rightCaptcha.equals(orgCaptcha)){
            rs.put("state","2");
            rs.put("message","验证码错误!");
            return rs;
        }

        Map<String,Object> map = new HashMap<>();
        map = orgLoginInfo;
        //密码转MD5
        String s = (String) map.get("password");
        map.put("password", DigestUtils.md5DigestAsHex(s.getBytes()));
        String lowerCaseEmail = ((String) orgLoginInfo.get("email")).toLowerCase(); //登入信息的邮箱变成小写
        map.put("email",lowerCaseEmail);
        System.out.println("账号信息"+map);
        Organization loginOrg = orgService.login(map);
       if (loginOrg!=null){
           rs.put("state","0");
           rs.put("message","登入成功!");
           rs.put("loginOrg",JSON.toJSONString(loginOrg));
           return rs;
       }else{
           rs.put("state","1");
           rs.put("message","账号或密码错误!");
           return rs;
       }
    }

    @RequestMapping("keepOrgLogin")
    public JSON keepOrgLogin(@RequestBody JSONObject loginInfo){
        JSONObject rs = new JSONObject();
        System.out.println("验证keepOrgLogin信息:"+loginInfo);
//        {"password":"202cb962ac59075b964b07152d234b70","email":"694886886@qq.com"}
        Organization loginOrg = orgService.keepOrgLogin(loginInfo);
        if (loginOrg!=null){
            rs.put("state","0");
            rs.put("message","登入成功!");
            rs.put("loginOrg",JSON.toJSONString(loginOrg));
            return rs;
        }else{
            rs.put("state","3");
            rs.put("message","您未登入或登入信息已失效!");
            return rs;
        }
    }



}
